package com.ebid.templates.tenn;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapUtil {


    public static Map<String ,Object> getData(String json){
        Map<String ,Object> map = new HashMap();

        ObjectMapper jackson = new ObjectMapper();

        try {
            JsonNode node = jackson.readTree(json);
            jsonLeaf(node,map);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return map;
    }



    public static void jsonLeaf(JsonNode node, Map map)
    {
        if (node.isValueNode())
        {
            return;
        }

        if (node.isObject())
        {
            Iterator<Map.Entry<String, JsonNode>> it = node.fields();
            while (it.hasNext())
            {
                Map.Entry<String, JsonNode> entry = it.next();
                if(entry.getValue() != null){

                    String value =entry.getValue().toString();
                    String relValue=value.isEmpty()|| value.length()==1 ? value: value.substring(1, value.length()-1);
                    System.out.println("relValue:"+relValue);

                    map.put(entry.getKey(),relValue);
                    jsonLeaf(entry.getValue(),map);
                }

            }
        }

        if (node.isArray())
        {
            if (node.size()==0){
                return;
            }
            Iterator<JsonNode> it = node.iterator();
            while (it.hasNext())
            {
                jsonLeaf(it.next(),map);
            }
        }
    }

    public static void main(String[] args) {
        String json = "{\n" +
                "\t\"TENDER_PROJECT_CODE\": \"M1100000015013191001\",\n" +
                "\t\"BID_SECTION_CODES\": [{\n" +
                "\t\t\"BID_SECTION_CODE\": \"M1100000015013191001001\"\n" +
                "\t}],\n" +
                "\n" +
                "\t\"SECTIONS_WIN_CANDIDATES\": [{\n" +
                "\t\t\t\"BID_SECTION_CODE\": \"M1100000015013191001001\",\n" +
                "\t\t\t\"WIN_CANDIDATES\": [{\n" +
                "\t\t\t\t\t\"WIN_CANDIDATE_CODE_TYPE\": \"97\",\n" +
                "\t\t\t\t\t\"WIN_CANDIDATE_CODE\": \"tongyi6568565656\",\n" +
                "\t\t\t\t\t\"WIN_CANDIDATE_NAME\": \"lwj测试专用变更\",\n" +
                "\t\t\t\t\t\"WIN_CANDIDATE_ORDER\": \"2\",\n" +
                "\t\t\t\t\t\"BID_RESULT\": {\n" +
                "\t\t\t\t\t\t\"PRICE\": {\n" +
                "\t\t\t\t\t\t\t\"BID_PRICE\": \"21.00\",\n" +
                "\t\t\t\t\t\t\t\"PRICE_CURRENCY\": \"23\"\n" +
                "\t\t\t\t\t\t}\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"QUALITY\": \"合格\",\n" +
                "\t\t\t\t\t\"EVALUATION_STATUS\": \"是\",\n" +
                "\t\t\t\t\t\"WIN_CANDIDATE_QUALIFICATION\": \"合格\",\n" +
                "\t\t\t\t\t\"TIME_LIMIT\": \"0\",\n" +
                "\t\t\t\t\t\"PM_NAME\": \"dc\",\n" +
                "\t\t\t\t\t\"PM_CODE\": \"17611260300\",\n" +
                "\t\t\t\t\t\"PM_CERTIFICATES\": \"142602199202031234\"\n" +
                "\t\t\t\t}\n" +
                "\n" +
                "\t\t\t]\n" +
                "\t\t}\n" +
                "\n" +
                "\t]\n" +
                "\n" +
                "}";

        Map<String,Object> map = getData(json);

        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Object> entry = (Map.Entry<String, Object>) iterator.next();
            System.out.println("key:"+entry.getKey()+"  value:" +entry.getValue());


        }


//        Map<String,Object> map1 = new HashMap<>();
        System.out.println(map.get("BID_SECTION_CODES").getClass());
        Map<String,String> map2 = (Map<String, String>) map.get("BID_SECTION_CODES");

        Iterator iteratorMap2 = map2.entrySet().iterator();
        while (iteratorMap2.hasNext()){
            Map.Entry entry = (Map.Entry) iteratorMap2.next();
            System.out.println(entry.getKey() +" "+ entry.getValue());
        }

     }
}
